<template>
  <div class="app-container">
    <el-descriptions class="margin-top" :column="3" border style="margin-bottom: 30px">
      <el-descriptions-item>
        <template slot="label">
          总积分
        </template>
        <span style="color: red">{{ memberPoint.totalPoint }}</span>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          可用积分
        </template>
        <span style="color: #96a8eb">{{ memberPoint.avaPoint }}</span>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          备注
        </template>
        {{ memberPoint.remark }}
      </el-descriptions-item>
    </el-descriptions>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账单类型 " prop="billType">
        <el-select v-model="queryParams.billType" placeholder="请选择账单类型 " clearable>
          <el-option
            v-for="dict in dict.type.bill_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型 " prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="请选择业务类型 " clearable>
          <el-option
            v-for="dict in dict.type.point_biz_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleGive"
          v-hasPermi="['uac:member-point-bill:give']"
        >赠送
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList">
      <el-table-column label="交易积分" align="center" prop="point"/>
      <el-table-column label="剩余积分" align="center" prop="avaPoint"/>
      <el-table-column label="账单类型 " align="center" prop="billType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_type" :value="scope.row.billType"/>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="bizType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.point_biz_type" :value="scope.row.bizType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 赠送会员积分对话框 -->
    <el-dialog title="赠送积分" :visible.sync="giveOpen" width="500px" append-to-body>
      <el-form ref="form" :model="giveForm" :rules="rules" label-width="80px">
        <el-form-item label="赠送积分" prop="point">
          <el-input v-model="giveForm.point" placeholder="请输入积分"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="giveForm.remark" placeholder="请输入备注" maxlength="120" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="giveOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBill} from "@/api/uac/memberPointBill";
import {getMemberPoint, givePoint} from '@/api/uac/memberPoint'

export default {
  name: "MemberPointBill",
  dicts: ['bill_type', 'point_biz_type'],
  props: {
    memberId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员积分账单表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      giveOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: this.memberId,
        billType: null,
        bizType: null,
      },
      memberPoint: {},
      giveForm: {
        memberId: this.memberId,
        point: 0,
        remark: ""
      }
    };
  },
  created() {
    this._getMemberPoint();
    this.getList();
  },
  methods: {
    _getMemberPoint() {
      getMemberPoint(this.memberId).then(response => {
        this.memberPoint = response.data;
      });
    },
    /** 查询会员积分账单列表 */
    getList() {
      this.loading = true;
      listBill(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 赠送按钮操作 */
    handleGive() {
      this.giveOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          givePoint(this.giveForm).then(response => {
            this.$modal.msgSuccess("提交成功");
            this.giveOpen = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
